home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / MacShell / AERequired.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-04  |  8.2 KB  |  329 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:        MacShell
  5. ** File:        AERequired.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. **
  11. ** This code implements the required AppleEvents, specific to MacShell.
  12. */
  13.  
  14.  
  15.  
  16. /*****************************************************************************/
  17.  
  18.  
  19.  
  20. #include "MacShell.h"            /* Get the MacShell includes/typedefs, etc.    */
  21. #include "MacShellCommon.h"        /* Get the stuff in common with rez.        */
  22. #include "MacShell.protos"        /* Get the prototypes for MacShell.            */
  23.  
  24. #ifndef __GESTALTEQU__
  25. #include <GestaltEqu.h>
  26. #endif
  27.  
  28. #ifdef THINK_C
  29. #include "Utilities.h"
  30. #else
  31. #ifndef __UTILITIES__
  32. #include <Utilities.h>
  33. #endif
  34. #endif
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41.  
  42. #define kTimeOutInTicks (60 * 30)    /* 30 second timeout. */
  43.  
  44.  
  45.  
  46. /*****************************************************************************/
  47.  
  48.  
  49.  
  50. static triplets keywordsToInstall[] = {
  51.     { kCoreEventClass,        kAEOpenApplication,        (ProcPtr)DoAEOpenApplication },
  52.     { kCoreEventClass,        kAEOpenDocuments,        (ProcPtr)DoAEOpenDocuments },
  53.     { kCoreEventClass,        kAEPrintDocuments,        (ProcPtr)DoAEPrintDocuments },
  54.     { kCoreEventClass,        kAEQuitApplication,        (ProcPtr)DoAEQuitApplication }
  55.         /* The above are the four required AppleEvents. */
  56. };
  57.  
  58. Boolean        gHasAppleEvents = false;
  59. Boolean        gHasPPCToolbox  = false;
  60.  
  61.  
  62.  
  63. /*****************************************************************************/
  64.  
  65.  
  66.  
  67. extern Boolean    gQuitApplication;
  68. extern Cursor    *gCursorPtr;
  69. extern short    gPrintPage;
  70.  
  71.  
  72.  
  73. /*****************************************************************************/
  74. /*****************************************************************************/
  75.  
  76.  
  77.  
  78. #pragma segment AppleEvents
  79. void    InitRequiredAppleEvents(void)
  80. {
  81.     OSErr    err;
  82.     long    result;
  83.     short    i;
  84.  
  85.     gHasPPCToolbox  = (Gestalt(gestaltPPCToolboxAttr, &result) ? false : result != 0);
  86.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? false : result != 0);
  87.  
  88.     if (gHasAppleEvents) {
  89.         for (i = 0; i < (sizeof(keywordsToInstall) / sizeof(triplets)); ++i) {
  90.             err = AEInstallEventHandler(
  91.                 keywordsToInstall[i].theEventClass,    /* What class to install.  */
  92.                 keywordsToInstall[i].theEventID,    /* Keywords to install.    */
  93.                 keywordsToInstall[i].theHandler,    /* The AppleEvent handler. */
  94.                 0L,                                    /* Unused refcon.           */
  95.                 false                                /* Only for our app.       */
  96.             );
  97.  
  98.             if (err) {
  99.                 Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
  100.                 return;
  101.             }
  102.         }
  103.     }
  104. }
  105.  
  106.  
  107.  
  108. /*****************************************************************************/
  109.  
  110.  
  111.  
  112. #pragma segment AppleEvents
  113. pascal OSErr    DoAEOpenApplication(AppleEvent *message, AppleEvent *reply, long refcon)
  114. {
  115. #pragma unused (message, refcon)
  116.  
  117.     FileRecHndl    frHndl;
  118.     OSErr        err;
  119.  
  120.     gCursorPtr = nil;
  121.         /* Force re-calc of cursor region and cursor to use. */
  122.  
  123.     err = AppNewDocument(&frHndl, docFileType);
  124.     if (!err)
  125.         if (err = DoNewWindow(frHndl, nil, (WindowPtr)-1, kwAppWindow))
  126.             AppDisposeDocument(frHndl);
  127.  
  128.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  129.         reply,                    /* The AppleEvent.              */
  130.         keyReplyErr,            /* AEKeyword                 */
  131.         typeShortInteger,        /* Desired type.             */
  132.         (Ptr)&err,                /* Pointer to area for data. */ 
  133.         sizeof(short)            /* Size of data area.         */
  134.     );
  135.  
  136.     return(err);
  137. }
  138.  
  139.  
  140.  
  141. /*****************************************************************************/
  142.  
  143.  
  144.  
  145. #pragma segment AppleEvents
  146. pascal OSErr    DoAEOpenDocuments(AppleEvent *message, AppleEvent *reply, long refcon)
  147. {
  148. #pragma unused (refcon)
  149.  
  150.     OSErr        err;
  151.  
  152.     gCursorPtr = nil;
  153.         /* Force re-calc of cursor region and cursor to use. */
  154.  
  155.     err = OpenDocEventHandler(message, reply, 0);
  156.  
  157.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  158.         reply,                    /* The AppleEvent.              */
  159.         keyReplyErr,            /* AEKeyword                 */
  160.         typeShortInteger,        /* Desired type.             */
  161.         (Ptr)&err,                /* Pointer to area for data. */ 
  162.         sizeof(short)            /* Size of data area.         */
  163.     );
  164.  
  165.     return(err);
  166. }
  167.  
  168.  
  169.  
  170. /*****************************************************************************/
  171.  
  172.  
  173.  
  174. #pragma segment AppleEvents
  175. pascal OSErr    DoAEPrintDocuments(AppleEvent *message, AppleEvent *reply, long refcon)
  176. {
  177. #pragma unused (refcon)
  178.  
  179.     OSErr        err;
  180.     short        openMode;
  181.  
  182.     gCursorPtr = nil;
  183.         /* Force re-calc of cursor region and cursor to use. */
  184.  
  185.     openMode = 1;
  186.     if (!AEInteractWithUser(kTimeOutInTicks, nil, nil))
  187.         ++openMode;
  188.  
  189.     err = OpenDocEventHandler(message, reply, openMode);
  190.  
  191.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  192.         reply,                    /* The AppleEvent.              */
  193.         keyReplyErr,            /* AEKeyword                 */
  194.         typeShortInteger,        /* Desired type.             */
  195.         (Ptr)&err,                /* Pointer to area for data. */ 
  196.         sizeof(short)            /* Size of data area.         */
  197.     );
  198.  
  199.     return(err);
  200. }
  201.  
  202.  
  203.  
  204. /*****************************************************************************/
  205.  
  206.  
  207.  
  208. #pragma segment AppleEvents
  209. pascal OSErr    DoAEQuitApplication(AppleEvent *message, AppleEvent *reply, long refcon)
  210. {
  211. #pragma unused (message, refcon)
  212.  
  213.     OSErr    err;
  214.  
  215.     gCursorPtr = nil;
  216.         /* Force re-calc of cursor region and cursor to use. */
  217.  
  218.     if (DisposeAllWindows()) {
  219.         gQuitApplication = true;
  220.         err = noErr;
  221.     }
  222.     else err = errAEEventNotHandled;
  223.  
  224.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  225.         reply,                    /* The AppleEvent.              */
  226.         keyReplyErr,            /* AEKeyword                 */
  227.         typeShortInteger,        /* Desired type.             */
  228.         (Ptr)&err,                /* Pointer to area for data. */ 
  229.         sizeof(short)            /* Size of data area.         */
  230.     );
  231.  
  232.     return(err);
  233. }
  234.  
  235.  
  236.  
  237. /*****************************************************************************/
  238.  
  239.  
  240.  
  241. /* OpenDocEventHandler
  242. **
  243. ** Called when we recieve an AppleEvent with an ID of "kAEOpenDocuments".
  244. ** This routine gets the direct parameter, parses it up into little FSSpecs,
  245. ** and opens each indicated file.  It also shows the technique to be used in
  246. ** determining if you are doing everything the AppleEvent record is telling
  247. ** you.  Parameters can be divided up into two groups: required and optional.
  248. ** Before executing an event, you must make sure that you've read all the
  249. ** required events.  This is done by making an "any more?" call to the
  250. ** AppleEvent manager.
  251. */
  252.  
  253. #pragma segment AppleEvents
  254. OSErr    OpenDocEventHandler(AppleEvent *message, AppleEvent *reply, short mode)
  255. {
  256. #pragma unused (reply)
  257.  
  258.     OSErr        err;
  259.     OSErr        err2;
  260.     AEDesc        theDesc;
  261.     FSSpec        theFSS;
  262.     short        loop;
  263.     long        numFilesToOpen;
  264.     AEKeyword    ignoredKeyWord;
  265.     DescType    ignoredType;
  266.     Size        ignoredSize;
  267.     FileRecHndl    frHndl;
  268.     WindowPtr    docWindow;
  269.  
  270.     theDesc.dataHandle = nil;
  271.         /* Make sure disposing of the descriptors is okay in all cases.
  272.         ** This will not be necessary after 7.0b3, since the calls that
  273.         ** attempt to create the descriptors will nil automatically
  274.         ** upon failure. */
  275.  
  276.     if (err = AEGetParamDesc(message, keyDirectObject, typeAEList, &theDesc))
  277.         return(err);
  278.  
  279.     if (!MissedAnyParameters(message)) {
  280.  
  281. /* Got all the parameters we need.  Now, go through the direct object,
  282. ** see what type it is, and parse it up. */
  283.  
  284.         err = AECountItems(&theDesc, &numFilesToOpen);
  285.         if (!err) {
  286.             /* We have numFilesToOpen that need opening, as either a window
  287.             ** or to be printed.  Go to it... */
  288.  
  289.             for (loop = 1; ((loop <= numFilesToOpen) && (!err)); ++loop) {
  290.                 err = AEGetNthPtr(        /* GET NEXT IN THE LIST...         */
  291.                     &theDesc,            /* List of file names.             */
  292.                     loop,                /* Item # in the list.             */
  293.                     typeFSS,            /* Item is of type FSSpec.         */
  294.                     &ignoredKeyWord,    /* Returned keyword -- we know.  */
  295.                     &ignoredType,        /* Returned type -- we know.     */
  296.                     (Ptr)&theFSS,        /* Where to put the FSSpec info. */
  297.                     sizeof(theFSS),        /* Size of the FSSpec info.         */
  298.                     &ignoredSize        /* Actual size -- we know.         */
  299.                 );
  300.                 if (err) break;
  301.  
  302.                 err = AppOpenDocument(&frHndl, &theFSS, fsRdWrPerm);
  303.                 if (err) break;
  304.  
  305.                 gPrintPage = mode;
  306.                     /* Open the window off-screen if we are printing. */
  307.                 if (err = DoNewWindow(frHndl, &docWindow, (WindowPtr)-1, kwAppWindow))
  308.                     AppDisposeDocument(frHndl);
  309.                 else {
  310.                     if (gPrintPage) {
  311.                         err  = AppPrintDocument(frHndl, (mode == 2), (loop == 1));
  312.                         mode = 1;
  313.                         AppDisposeDocument(frHndl);
  314.                         DisposeAnyWindow(docWindow);
  315.                     }
  316.                 }
  317.                 gPrintPage = 0;
  318.             }
  319.         }
  320.     }
  321.     AppPrintDocument(nil, false, false);    /* Clean up after printing, if we did any. */
  322.  
  323.     err2 = AEDisposeDesc(&theDesc);
  324.     return(err ? err : err2);
  325. }
  326.  
  327.  
  328.  
  329.